<template>
  <div class="container">
    <el-row>
      <el-card shadow="never">
        <div slot="header">
          <el-form :inline="true" :model="searchForm" size="small">
            <el-form-item label="ID" class="el-search-item">
              <el-input v-model="searchForm.contentCollectionId" placeholder="ID" clearable></el-input>
            </el-form-item>
            <el-form-item label="创建时间" class="el-search-item">
              <el-date-picker v-model="checkedTime" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item class="el-search-item">
              <el-button type="success" plain icon="el-icon-search" @click="getDataList">查询</el-button>
            </el-form-item>
            <el-form-item class="el-search-item">
              <el-button v-if='hasPerm("content:contentCollection:add")' type="primary" plain icon="el-icon-plus" @click="openAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--数据表格 element ui-->
        <el-table :data="tableData" :height="tableHeight" style="width: 100%" v-loading="tableLoading" element-loading-text="数据正在加载中...">
          <el-table-column prop="contentCollectionId" label="用户收藏" align="center" sortable></el-table-column>
          <el-table-column prop="customerId" label="收藏人" align="center" sortable></el-table-column>
          <el-table-column prop="companyId" label="企业" align="center" sortable></el-table-column>
          <el-table-column prop="contentId" label="被收藏对象的id 为适配性用长整形" align="center" sortable></el-table-column>
          <el-table-column prop="msg" label="收藏说明" align="center" sortable></el-table-column>
          <el-table-column prop="createTime" label="收藏时间" align="center" sortable></el-table-column>
          <el-table-column v-if='hasPerm("content:contentCollection:list")' label="更多操作" align="center" width="250">
            <template slot-scope="scope">
              <el-button v-if='hasPerm("content:contentCollection:edit")' @click="openUpdate(scope.row)" size="mini" type="text" icon="el-icon-edit">编辑</el-button>
              <el-button v-if='hasPerm("content:contentCollection:del")' @click="deleteFun(scope.row)" size="mini" type="text" icon="el-icon-delete">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination style="padding:0;text-align: center;" background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper"
                       :current-page="pageNum" :page-sizes="[10, 20, 30]" :page-size="pageSize" :total="total">
        </el-pagination>
      </el-card>
      <!--新增 编辑对话框-->
    <el-drawer :title="dialog.dialogTitle" :visible.sync="dialog.visible" :direction="dialog.direction" :size="dialog.size" :before-close="handleClose">
    <el-row class="drawer-table">
      <el-col :span="24">
        <el-form :rules="rules" ref="contentCollectionForm" :model="contentCollectionForm" size="small" label-width="80px">
          <el-form-item prop="contentCollectionId" label="用户收藏">
            <el-input v-model="contentCollectionForm.contentCollectionId" placeholder="请输入用户收藏" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="customerId" label="收藏人">
            <el-input v-model="contentCollectionForm.customerId" placeholder="请输入收藏人" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="companyId" label="企业">
            <el-input v-model="contentCollectionForm.companyId" placeholder="请输入企业" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="contentId" label="被收藏对象的id 为适配性用长整形">
            <el-input v-model="contentCollectionForm.contentId" placeholder="请输入被收藏对象的id 为适配性用长整形" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="msg" label="收藏说明">
            <el-input v-model="contentCollectionForm.msg" placeholder="请输入收藏说明" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="createTime" label="收藏时间">
            <el-input v-model="contentCollectionForm.createTime" placeholder="请输入收藏时间" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="drawer-footer">
      <el-col :span="24">
        <el-button @click="dialog.visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submitFun()" :loading="loading.status" size="small">{{loading.status ? loading.SubText : loading.BtnText}}</el-button>
      </el-col>
    </el-row>
    </el-drawer>

    </el-row>
  </div>
</template>
<script>
  import content_contentCollection from "@/api/content/contentCollection";
  export default {
    data() {
      return {
        // 表格数据
        tableData: [],
        tableLoading: false,
        // 分页数据
        total: 0,
        pageNum: 1,
        pageSize: 10,
        // 表格高度
        tableHeight: 0,
        // 用来存放日期组件所选中的值
        checkedTime: '',
        // 查询参数
        searchForm: {contentCollectionId: null, startTime: null, endTime: null,},
        // 弹出层
        dialog: {flag: 0,dialogTitle: "",visible: false,direction: 'rtl',size: '35%'},
        // 表单参数
        contentCollectionForm: {
          contentCollectionId: null,
          customerId: null,
          companyId: null,
          contentId: null,
          msg: null,
          createTime: null,
        },
        // 提交按钮文字
        loading: {status: false,BtnText: '确 定',SubText: '提交中...'},
        // 表单校验
        rules: {
          contentCollectionId: [{required: true,trigger: "change",message: "请输入用户收藏"}],
          customerId: [{required: true,trigger: "change",message: "请输入收藏人"}],
          companyId: [{required: true,trigger: "change",message: "请输入企业"}],
          contentId: [{required: true,trigger: "change",message: "请输入被收藏对象的id 为适配性用长整形"}],
          msg: [{required: true,trigger: "change",message: "请输入收藏说明"}],
          createTime: [{required: true,trigger: "change",message: "请输入收藏时间"}],
        },
      };
    },
    mounted() {
      this.$nextTick(() => {
        this.tableHeight = window.innerHeight - 205;
      });
    },
    created() {
      this.getDataList();
    },
    watch: {
      "checkedTime"(value) {
        this.searchForm.startTime = null;
        this.searchForm.endTime = null;
        if (value != null) {
          let timeArr = value.toString().split(",");
          timeArr.forEach((item,i) => {
            if(i === 0) {
              this.searchForm.startTime = item;
            }else {
              this.searchForm.endTime = item;
            }
          });
        }
      }
    },
    methods: {
      handleClose(done) {
        this.resetForm();
        done();
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.getDataList();
      },
      handleCurrentChange(val) {
        this.pageNum = val;
        this.getDataList();
      },
      // 获取列表
      getDataList() {
        this.tableLoading = true;
        content_contentCollection.list({
          current: this.pageNum, size: this.pageSize, contentCollectionId: this.searchForm.contentCollectionId,
          startTime: this.searchForm.startTime, endTime: this.searchForm.endTime
        }).then(res => {
          if(res.data.code === 2000) {
            this.tableData = res.data.data.records;
            this.total = res.data.data.total;
          }
          this.tableLoading = false;
        });
      },
      // 新增按钮操作
      openAdd() {
        this.dialog.flag = 0;
        this.resetForm("contentCollectionForm");
        this.dialog.dialogTitle = "新增";
        this.dialog.visible = true;
      },
      // 更新按钮操作
      openUpdate(row) {
        this.dialog.flag = 1;
        this.resetForm("contentCollectionForm");
        this.dialog.dialogTitle = "编辑";
        this.dialog.visible = true;
        this.getById(row.contentCollectionId);
      },
      // 提交数据操作
      submitFun() {
        this.$refs.contentCollectionForm.validate(valid => {
          if(valid){
            this.loading.status = true;
            content_contentCollection.saveOrUpdate(this.contentCollectionForm,this.dialog.flag).then(res => {
              if (res.data.code === 2000) {
                this.$message.success(res.data.msg);
                if(this.dialog.flag === 0){
                  this.pageNum = 1;
                }
                this.getDataList();
                // 关闭弹框
                this.dialog.visible = false;
                // 关闭加载
                this.loading.status = false;
              }else{
                this.$message.error(res.data.msg);
                this.loading.status = false;
                return false;
              }
            });
          }
        });
      },
      // 根据id获取信息
      getById(id) {
        content_contentCollection.getById(id).then(res => {
          if (res.data.code === 2000) {
            this.contentCollectionForm.contentCollectionId = res.data.data.contentCollectionId;
            this.contentCollectionForm.customerId = res.data.data.customerId;
            this.contentCollectionForm.companyId = res.data.data.companyId;
            this.contentCollectionForm.contentId = res.data.data.contentId;
            this.contentCollectionForm.msg = res.data.data.msg;
            this.contentCollectionForm.createTime = res.data.data.createTime;
          }
        });
      },
      // 根据id删除
      deleteFun(row) {
        this.$confirm("确定删除当前吗？", "系统提示", {confirmButtonText: "确定", cancelButtonText: "取消", type: 'error'
        }).then(() =>{
            content_contentCollection.remove(row.contentCollectionId).then(res => {
            if(res.data.code === 2000){
              this.$message.success(res.data.msg);
              this.getDataList();
            }else {
              this.$message.error(res.data.msg);
            }
          });
        }).catch(() => {
          this.$message.info("取消删除");
        });
      },
      // 清空表单
      resetForm(formName) {
        if (this.$refs[formName]) {
          this.$refs[formName].resetFields();
        }
      },
    }
  }
</script>
<style lang="less" scoped>
  .el-search-item {margin-bottom: 0;}
  .el-card/deep/.el-card__body {padding: 0 10px 10px 10px !important;}
  .el-card/deep/.el-card__header {border-bottom: none !important;padding: 10px 10px !important;}
</style>